package cn.stylefeng.guns.modular.order.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.basic.entity.BasicCustomer;
import cn.stylefeng.guns.modular.basic.service.BasicCustomerService;
import cn.stylefeng.guns.modular.order.entity.OutOrderDetailed;
import cn.stylefeng.guns.modular.order.mapper.OutOrderDetailedMapper;
import cn.stylefeng.guns.modular.order.model.params.OutOrderDetailedParam;
import cn.stylefeng.guns.modular.order.model.result.OutOrderDetailedResult;
import  cn.stylefeng.guns.modular.order.service.OutOrderDetailedService;
import cn.stylefeng.guns.modular.storage.service.StorageStockService;
import cn.stylefeng.guns.sys.modular.system.entity.User;
import cn.stylefeng.guns.sys.modular.system.service.UserService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

/**
 * 出库订单明细表 服务实现类
 */
@Service
public class OutOrderDetailedServiceImpl extends ServiceImpl<OutOrderDetailedMapper, OutOrderDetailed> implements OutOrderDetailedService {

    @Autowired
    private BasicCustomerService basicCustomerService;
    @Autowired
    private UserService userService;
    @Autowired
    private StorageStockService storageStockService;
    @Override
    public void add(OutOrderDetailedParam param){
        OutOrderDetailed entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(OutOrderDetailedParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(OutOrderDetailedParam param){
        OutOrderDetailed oldEntity = getOldEntity(param);
        OutOrderDetailed newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public OutOrderDetailedResult findBySpec(OutOrderDetailedParam param){
        return this.baseMapper.findBySpec(param);
    }

    @Override
    public List<OutOrderDetailedResult> findListBySpec(OutOrderDetailedParam param){
        return this.baseMapper.findListBySpec(param);
    }

    @Override
    public LayuiPageInfo findPageBySpec(OutOrderDetailedParam param,String condition){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param,condition);
        User user = userService.getcurrentUser();
//        if(user.getDeptId() == 1922549501895528450L){
//            BasicCustomer basicCustomer = basicCustomerService.getById(132l);
//            if("Yes".equals(basicCustomer.getRestockingLimit())){
                List<OutOrderDetailedResult> records = page.getRecords();
                records.forEach(item -> {
                    String kcCount = storageStockService.getKCCountByGoodsNumber(item.getDetailedGoodscode(),null);
                    String jhkcCount = storageStockService.getKCCountByGoodsNumber(item.getDetailedGoodscode(),"Picking location");
                    String cckcCount = storageStockService.getKCCountByGoodsNumber(item.getDetailedGoodscode(),"Storage location");
                    String djhCount = storageStockService.getDjhCountByGoodsNumber(item.getDetailedGoodscode(),user.getDeptId());
                    item.setKcCount(kcCount);
                    item.setJhkcCount(jhkcCount);
                    item.setCckcCount(cckcCount);
                    item.setDjhCount(djhCount);
                });
//            }
//        }

        return LayuiPageFactory.createPageInfo(page);
    }

    private Serializable getKey(OutOrderDetailedParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private OutOrderDetailed getOldEntity(OutOrderDetailedParam param) {
        return this.getById(getKey(param));
    }

    private OutOrderDetailed getEntity(OutOrderDetailedParam param) {
        OutOrderDetailed entity = new OutOrderDetailed();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

	@Override
	public List<OutOrderDetailedResult> findListGroupGoodsidBySpec(OutOrderDetailedParam outParam) {
		return this.baseMapper.findListGroupGoodsidBySpec(outParam);
	}

	@Override
	public boolean saveBatchList(List<OutOrderDetailedParam> list) {
		return this.baseMapper.saveBatchList(list);
	}

	@Override
	public List<OutOrderDetailedResult> findListBySpecsss(OutOrderDetailedParam param) {
		return this.baseMapper.findListBySpecsss(param);
	}

	@Override
	public List<OutOrderDetailedResult> findOrderDetailsBystorageOutNumber(OutOrderDetailedParam param, String[] strs) {
		return this.baseMapper.findOrderDetailsBystorageOutNumber(param,strs);
	}

}
